//
//  SEGIntegralCashViewController.m
//  ExcellentHome
//
//  Created by 刘山 on 2017/2/16.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGIntergralCashCell.h"
#import "SEGIntergralBalanceCell.h"
#import "SEGBalanceForHbModel.h"
#import "SEGIntegralManager.h"

#import "SEGIntegralCashViewController.h"

#if __has_include(<Commom/CommomHeader.h>)
#import <Commom/CommomHeader.h>
#else
#import "CommomHeader.h"
#endif

//#if __has_include(<SVProgressHUD/SVProgressHUD.h>)
//#import <SVProgressHUD/SVProgressHUD.h>
//#else
//#import "SVProgressHUD.h"
//#endif

#if __has_include(<CommonMediator/SEGMediatorHeader.h>)
#import <CommonMediator/SEGMediatorHeader.h>
#else
#import "SEGMediatorHeader.h"
#endif


#if __has_include(<Commom/SEGUrlAddress.h>)
#import <Commom/SEGUrlAddress.h>
#else
#import "SEGUrlAddress.h"
#endif

#import "SEGMenberPointsViewModel.h"

@interface SEGIntegralCashViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)SEGMenberPointsViewModel* viewModel;
@end

@implementation SEGIntegralCashViewController{
    UIScrollView *scrollView_;
    UILabel *totalLabel;
    UILabel *cashLabel;
    UIImageView *imgeView;
    UIImageView *iconimgeView; // 背景图片
    UIButton *help;
    
    UILabel *integralTodayCash;//今日累积金额
    UILabel *integralYesTodayCash;//昨日累积金额
    
    UITableView *cash_tableView;
    UITableView *balance_tableView;
    
    NSString* balance;
}



- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kColor_bg;
    balance = @"";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    
    [self segInitViews];
    [self bindViewModel];
}

-(void)makeNaviLineToColor:(UIColor*)color{
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    [navigationBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [navigationBar setShadowImage:[self imageWithColor:color]];
}

-(void)makeNaviLineToNormal{
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    [navigationBar setBackgroundImage:nil forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [navigationBar setShadowImage:nil];
}

- (UIImage *)imageWithColor:(UIColor *)color{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext(); return image;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (self.banalceType == SEGBanalceType_Integral) {
        
        [self showCustomNavigationBackButton1];
        
        [self.navigationController.navigationBar setTitleTextAttributes:@{
            NSFontAttributeName:[UIFont systemFontOfSize:15],
            NSForegroundColorAttributeName:[UIColor whiteColor]
        }];
        
        self.navigationController.navigationBar.backgroundColor = kColor_S1;
        [self makeNaviLineToColor:kColor_S1];
    }
    
    if (self.banalceType == SEGBanalceType_Cash || self.banalceType == SEGBanalceType_Integral) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:kColor_S1] forBarMetrics:(UIBarMetricsDefault)];
        if (@available(iOS 13.0, *)) {
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.backgroundColor = [UIColor clearColor];
            titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
            titleLabel.textColor = kColor_W;
            titleLabel.text = self.title;
            [titleLabel sizeToFit];
            self.navigationItem.titleView = titleLabel;
            self.navigationController.navigationBar.standardAppearance.backgroundColor = kColor_S1;
            self.navigationController.navigationBar.standardAppearance.shadowColor = [UIColor clearColor];
        }
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBar.backgroundColor = kColor_bg;
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{
        NSFontAttributeName:[UIFont systemFontOfSize:15],
        NSForegroundColorAttributeName:kColor_1
    }];
    
    [self.navigationController.navigationBar setBackgroundImage:nil forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    if (@available(iOS 13.0, *)) {
        self.navigationController.navigationBar.standardAppearance.backgroundColor = kColor_W;
    }
}


/// UI-头部
-(void)headView{
    [self makeNaviLineToColor:kColor_S1];
    
    scrollView_ = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight() - kNavigationBarAndStatusBarHeight)];
    scrollView_.backgroundColor = kColor_bg;
    scrollView_.contentSize = scrollView_.bounds.size;
    scrollView_.delegate = self;
    [self.view addSubview:scrollView_];
    
    //红色背景
    imgeView = [[UIImageView alloc]init];
    [imgeView sizeToFit];
    imgeView.backgroundColor = kColor_S1;
    imgeView.frame = CGRectMake(CGRectGetMinX(scrollView_.frame), CGRectGetMinY(scrollView_.frame), CGRectGetWidth(scrollView_.frame), 40);
    [scrollView_ addSubview:imgeView];
    
    //红色背景
    iconimgeView = [[UIImageView alloc]init];
    iconimgeView.image = [UIImage imageWithContentsOfFile: self.banalceType == SEGBanalceType_Integral? SEGPicName(@"img_pocketmoney_bg_02") : SEGPicName(@"img_pocketmoney_bg_01")];
    [iconimgeView sizeToFit];
    [imgeView addSubview:iconimgeView];
    
    //统计
    totalLabel = [UILabel labelWithFrame:CGRectMake(15, 15, 1, 1) WithFont:[UIFont systemFontOfSize:13] WithTag:1001 WithTextColor:kColor_W WithTextAlignment:NSTextAlignmentLeft WithText:@"零钱总额(元)"];
    CGSize size = [totalLabel.text boundingRectWithContentWidth:LYScreenWidth() WithFontOfSize:13];
    [totalLabel setFrame:CGRectMake(CGRectGetMinX(totalLabel.frame), CGRectGetMinY(totalLabel.frame), size.width, size.height)];
    [imgeView addSubview:totalLabel];
    
    //帮助中心
    help = [UIButton buttonWithFrame:CGRectMake(0, 0, 0, 0) TitleFont:kFont_13 Tag:1003 TitleNormal:@"兑换记录" TitleSelected:nil BackgroundColor:nil TitleColorNormal:kColor_W TitleColorSelected:nil BackgroundImageNormal:nil BackgroundImageSelected:nil ImageNormal:nil ImageSelected:nil];
    [scrollView_ addSubview:help];
    [help addTarget:self action:@selector(helpClick) forControlEvents:UIControlEventTouchUpInside];
    help.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//文字居左
    UIImage *rightImage = [UIImage imageWithContentsOfFile:SEGPicName(@"img_pocketmoney_arrow_w")];
    NSDictionary *attrs = @{NSFontAttributeName : kFont_13};
    CGSize helpTextSize = [help.titleLabel.text sizeWithAttributes:attrs];
    CGSize helpButtonSize = CGSizeMake(helpTextSize.width + 4 + rightImage.size.width, helpTextSize.height);
    UIImageView *rightImageView = [[UIImageView alloc] initWithImage:rightImage];
    rightImageView.frame = CGRectMake(helpButtonSize.width - rightImage.size.width, (helpButtonSize.height - rightImage.size.height) / 2.0, rightImage.size.width, rightImage.size.height);
    [help addSubview:rightImageView];
    
    
    //总金额
    cashLabel = [UILabel labelWithFrame:CGRectMake(CGRectGetMinX(totalLabel.frame), CGRectGetMaxY(totalLabel.frame) + 22 - 7.5, 1, 1) WithFont:[UIFont systemFontOfSize:49] WithTag:1002 WithTextColor:kColor_W WithTextAlignment:NSTextAlignmentLeft WithText:@"0"];
    CGSize cashSize = [cashLabel.text boundingRectWithContentWidth:LYScreenWidth() WithFontOfSize:49];
    [cashLabel setFrame:CGRectMake(CGRectGetMinX(cashLabel.frame), CGRectGetMinY(cashLabel.frame), cashSize.width, cashSize.height)];
    [imgeView addSubview:cashLabel];
    
    imgeView.frame = CGRectMake(CGRectGetMinX(imgeView.frame), CGRectGetMinY(imgeView.frame) , CGRectGetWidth(imgeView.frame), CGRectGetMaxY(cashLabel.frame) + 20 - 7.5 + 3);
    
    iconimgeView.frame = CGRectMake(
                                    CGRectGetMinX(imgeView.frame) +  (CGRectGetWidth(imgeView.frame) - CGRectGetWidth(iconimgeView.frame)),
                                    CGRectGetMinY(imgeView.frame) +  (CGRectGetHeight(imgeView.frame) - CGRectGetHeight(iconimgeView.frame)),
                                    CGRectGetWidth(iconimgeView.frame),
                                    CGRectGetHeight(iconimgeView.frame)
                                    );
    
    help.frame = CGRectMake(CGRectGetWidth(scrollView_.frame) - (helpButtonSize.width + 15),
                            CGRectGetMaxY(iconimgeView.frame) - 20 - helpButtonSize.height,
                            helpButtonSize.width,
                            helpButtonSize.height);
}

/// UI-零钱
-(void)cachView{
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imgeView.frame) + 10, LYScreenWidth(), 44)];
    backgroundView.backgroundColor = kColor_W;
    [scrollView_ addSubview:backgroundView];
    
    //零钱记录标示
    UILabel *desc = [UILabel labelWithFrame:CGRectMake(15, 0, 0, 0) WithFont:[UIFont systemFontOfSize:13] WithTag:1004 WithTextColor:kColor_1 WithTextAlignment:NSTextAlignmentLeft WithText:@"零钱记录"];
    CGSize descSize = [desc.text boundingRectWithContentWidth:LYScreenWidth() WithFontOfSize:13];
    desc.frame = CGRectMake(15, (CGRectGetHeight(backgroundView.frame) - descSize.height)*0.5, descSize.width, descSize.height);
    [backgroundView addSubview:desc];
    
    //中间线
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMinX(backgroundView.frame) + 15, CGRectGetHeight(backgroundView.frame) - 0.5, CGRectGetWidth(backgroundView.frame) - 15, 0.5)];
    lineView.backgroundColor = kColor_L2;
    [backgroundView addSubview:lineView];
    
    cash_tableView = [[UITableView alloc]initWithFrame:CGRectMake(CGRectGetMinX(backgroundView.frame), CGRectGetMaxY(backgroundView.frame), LYScreenWidth(), 44) style:UITableViewStylePlain];
    [cash_tableView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
    cash_tableView.delegate = self;
    cash_tableView.dataSource = self;
    cash_tableView.rowHeight =  UITableViewAutomaticDimension;
    cash_tableView.estimatedRowHeight = 240;
    [scrollView_ addSubview:cash_tableView];
}

-(void)dealloc{
    [cash_tableView removeObserver:self forKeyPath:@"contentSize"];
}

/// UI-优惠券
-(void)balanceView{
    balance_tableView = [[UITableView alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.view.frame), CGRectGetMinY(self.view.frame), CGRectGetWidth(self.view.frame), LYScreenHeight() - kNavigationBarAndStatusBarHeight) style:UITableViewStylePlain];
    balance_tableView.delegate = self;
    balance_tableView.dataSource = self;
    balance_tableView.rowHeight =  UITableViewAutomaticDimension;
    balance_tableView.estimatedRowHeight = 240;
    balance_tableView.backgroundColor = kColor_bg;
    balance_tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:balance_tableView];
}

/// UI-积分
-(void)integralView{
    
    
    //白色背景
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imgeView.frame), LYScreenWidth(), 44)];
    backgroundView.backgroundColor = kColor_W;
    [scrollView_ addSubview:backgroundView];
    
    //今日累计
    UILabel *integralToday = [UILabel labelWithFrame:CGRectMake(15, 15, 1, 1) WithFont:[UIFont systemFontOfSize:13] WithTag:1005 WithTextColor:kColor_3 WithTextAlignment:NSTextAlignmentLeft WithText:@"今日累计"];
    CGSize sizeToday = [integralToday.text boundingRectWithContentWidth:LYScreenWidth() WithFontOfSize:13];
    [integralToday setFrame:CGRectMake(15, 15 - 1.5, sizeToday.width, sizeToday.height)];
    [backgroundView addSubview:integralToday];
    
    //今日累计数额
    integralTodayCash = [UILabel labelWithFrame:CGRectMake(0, 0, 0, 0) WithFont:[UIFont systemFontOfSize:16] WithTag:1005 WithTextColor:kColor_1 WithTextAlignment:NSTextAlignmentLeft WithText:@"0"];
    CGSize sizeTodayCash = [integralTodayCash.text boundingRectWithContentWidth:LYScreenWidth() WithFontOfSize:16];
    [integralTodayCash setFrame:CGRectMake(CGRectGetMinX(integralToday.frame), CGRectGetMaxY(integralToday.frame) + 10 -4, sizeTodayCash.width, sizeTodayCash.height)];
    [backgroundView addSubview:integralTodayCash];
    
    //中间线
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMidX(backgroundView.frame), 15, 0.5, CGRectGetHeight(backgroundView.frame) - 30)];
    lineView.backgroundColor = kColor_L2;
    [backgroundView addSubview:lineView];
    
    //昨日累计
    UILabel * integralYestoday = [UILabel labelWithFrame:CGRectMake(CGRectGetMaxX(lineView.frame) + 15, 15, 1, 1) WithFont:[UIFont systemFontOfSize:13] WithTag:1005 WithTextColor:kColor_3 WithTextAlignment:NSTextAlignmentLeft WithText:@"昨日累计"];
    CGSize sizeYesYestoday = [integralYestoday.text boundingRectWithContentWidth:LYScreenWidth() WithFontOfSize:13];
    [integralYestoday setFrame:CGRectMake(CGRectGetMinX(integralYestoday.frame), CGRectGetMinY(integralYestoday.frame), sizeYesYestoday.width, sizeYesYestoday.height)];
    [backgroundView addSubview:integralYestoday];
    
    //昨日累计数额
    integralYesTodayCash = [UILabel labelWithFrame:CGRectMake(0, 0, 0, 0) WithFont:[UIFont systemFontOfSize:16] WithTag:1005 WithTextColor:kColor_1 WithTextAlignment:NSTextAlignmentLeft WithText:@"0"];
    CGSize sizeYesTodayCash = [integralYesTodayCash.text boundingRectWithContentWidth:LYScreenWidth() WithFontOfSize:16];
    [integralYesTodayCash setFrame:CGRectMake(CGRectGetMaxX(lineView.frame) + 15, CGRectGetMaxY(integralYestoday.frame) + 10 - 4, sizeYesTodayCash.width, sizeYesTodayCash.height)];
    integralYesTodayCash.font = [UIFont systemFontOfSize:16];
    [backgroundView addSubview:integralYesTodayCash];
    
    backgroundView.frame = CGRectMake(CGRectGetMinX( backgroundView.frame), CGRectGetMinY( backgroundView.frame), CGRectGetWidth( backgroundView.frame), CGRectGetMaxY(integralYesTodayCash.frame) + 15 - 6);
    lineView.frame = CGRectMake(CGRectGetMinX( lineView.frame), CGRectGetMinY( lineView.frame), CGRectGetWidth( lineView.frame), CGRectGetHeight(backgroundView.frame) - 30);
}

// 设置监听
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    if ([keyPath isEqualToString:@"contentSize"]) {
        [scrollView_ setContentSize:CGSizeMake(LYScreenWidth(), cash_tableView.contentSize.height  + CGRectGetHeight(self.view.frame))];
        cash_tableView.frame = CGRectMake(CGRectGetMinX(cash_tableView.frame), CGRectGetMinY(cash_tableView.frame), CGRectGetWidth(cash_tableView.frame), cash_tableView.contentSize.height);
    }
}

#pragma mark - delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (self.banalceType == SEGBanalceType_Banalce_Cash || self.banalceType == SEGBanalceType_Banalce_Time) {
        return;
    }
    
    /*
     [UIColor colorWithRed:240.0/255.0 green:239.0/255.0 blue:244.0/255.0 alpha:1] 背景色
     [UIColor colorWithRed:255.f/255.f green: 88.0/255.f blue: 88.0/255.f alpha:1] 红色
     [UIColor colorWithRed:255.f/255.f green:255.f/255.f blue:255.f/255.f alpha:1] 白色
     
     0   - 167
     88  - 255
     255(255-0-88) - 0(255-167-88)     255 - 167 = 88
     */
    
    if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y <= 167) {
        [self showCustomNavigationBackButton1];
        self.navigationController.navigationBar.backgroundColor = [UIColor
                                                                   colorWithRed:255.f/255.f
                                                                   green: (88.0 + scrollView.contentOffset.y)/255.f
                                                                   blue:(88.0 + scrollView.contentOffset.y)/255.f
                                                                   alpha: 1];
        
        [self.navigationController.navigationBar setTitleTextAttributes:@{
            NSFontAttributeName:[UIFont systemFontOfSize:15],
            NSForegroundColorAttributeName:[UIColor
                                            colorWithRed:(255.f  - scrollView.contentOffset.y)/255.f
                                            green: (255.f  - scrollView.contentOffset.y)/255.f
                                            blue:(255.f  - scrollView.contentOffset.y)/255.f
                                            alpha: 1]
        }];
        
        [self makeNaviLineToColor:[UIColor
                                   colorWithRed:255.f/255.f
                                   green: (88.0 + scrollView.contentOffset.y)/255.f
                                   blue:(88.0 + scrollView.contentOffset.y)/255.f
                                   alpha: 1]];
    }else if(scrollView.contentOffset.y < 0){
        
        self.navigationController.navigationBar.backgroundColor = kColor_S1;
        [self.navigationController.navigationBar setTitleTextAttributes:@{
            NSFontAttributeName:[UIFont systemFontOfSize:15],
            NSForegroundColorAttributeName:kColor_W
        }];
        
        [self makeNaviLineToColor:kColor_S1];
    }else{
        self.navigationController.navigationBar.backgroundColor = kColor_W;
        [self showCustomNavigationBackButton];
        [self.navigationController.navigationBar setTitleTextAttributes:@{
            NSFontAttributeName:[UIFont systemFontOfSize:15],
            NSForegroundColorAttributeName:kColor_1                                                                     }];
        
        [self makeNaviLineToNormal];
    }
}

//获取积分成长值
-(void)loadDataintegral{
    [self.viewModel loadDataintegral:kAccountUserID completed:^(NSString * _Nonnull all, NSString * _Nonnull today, NSString * _Nonnull yesterday) {
        //积分总数
        self->cashLabel.text = all;;
        [self->cashLabel sizeToFit];
        
        //今日累计
        self->integralTodayCash.text = today;
        [self->integralTodayCash sizeToFit];
        
        //昨日累计
        self->integralYesTodayCash.text = yesterday;
        [self->integralYesTodayCash sizeToFit];
    }];
}

//获取 零钱、优惠券 总数
-(void)loadData_Integral{
    [self.viewModel loadData_Integral:^{
        if (self.banalceType == SEGBanalceType_Banalce_Cash || self.banalceType == SEGBanalceType_Banalce_Time) {
            if (self.viewModel.dataSource.count > 0 ) { // 没有数据显示遮罩层
                [self->balance_tableView reloadData];
                [[SEGUhomeMediator sharedInstance]seg_hideCustomProgressImage:@{@"view":self.view}];
                [self balanceView];
                [self->balance_tableView reloadData];
            }
            else{
                [self s2];
            }
        }
    }];
}

-(void)loadBalanceForHb{
    [self s3];
    cashLabel.text = [NSString stringWithFormat:@"0.00"];
    [cashLabel sizeToFit];
    
    [self.viewModel loadBalanceForHb:^(BOOL hasRecord, CGFloat cashTotal) {
        
        self->cashLabel.text = [NSString stringWithFormat:@"%.2f",cashTotal / 100.F];
        [self->cashLabel sizeToFit];
        [self->cash_tableView reloadData];
        
        if (!hasRecord) {
            //显示 暂无零钱记录
            UIView *noRecordView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self->imgeView.frame), LYScreenWidth(), CGRectGetMaxY(self->scrollView_.frame)-CGRectGetMaxY(self->imgeView.frame))];
            noRecordView.backgroundColor = kColor_bg;
            [self->scrollView_ addSubview:noRecordView];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(noRecordView.frame) / 2.0 - 10, CGRectGetWidth(noRecordView.frame), 20)];
            label.text = @"暂无零钱记录";
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = kColor_3;
            label.font = kFont_13;
            [noRecordView addSubview:label];
        }
    }];
}

-(void)s2{
    self.navigationController.navigationBar.backgroundColor = kColor_bg;
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{
        NSFontAttributeName:[UIFont systemFontOfSize:15],
        NSForegroundColorAttributeName:kColor_1
    }];
    
    
    [self showCustomNavigationBackButton];
    [[SEGUhomeMediator sharedInstance]seg_showCustomProgressImage:@{@"view":self.view,@"text":kNoDataDefaultMessage}];
}

-(void)s3{
    [[SEGUhomeMediator sharedInstance]seg_hideCustomProgressImage:@{@"view":self.view}];
    [self headView];
    [self cachView];
    [help setHidden:YES];
    
    
    [self showCustomNavigationBackButton1];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{
        NSFontAttributeName:[UIFont systemFontOfSize:15],
        NSForegroundColorAttributeName:kColor_W
    }];
    
    self.navigationController.navigationBar.backgroundColor = kColor_S1;
}

-(void)helpClick{
    //兑换记录
    NSDictionary *params = @{@"linkURL":kIntegarlExchangeRecord};
    UIViewController *WebViewLoadVC = [[SEGUhomeMediator sharedInstance] seg_initWebViewLoadController:params];
    [self.navigationController pushViewController:WebViewLoadVC animated:YES];
}

#pragma mark - UIScrollViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.banalceType == SEGBanalceType_Cash) {
        return CGFLOAT_MIN;
    }
    return 5.0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.banalceType == SEGBanalceType_Cash && self.viewModel.managerIntegral.seg_segbalanceforhbmodel.count > 0) {
        return 1;
    }else if ((self.banalceType == SEGBanalceType_Banalce_Cash || self.banalceType == SEGBanalceType_Banalce_Time) && self.viewModel.dataSource.count > 0){
        return self.viewModel.dataSource.count;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.banalceType == SEGBanalceType_Cash && self.viewModel.managerIntegral.seg_segbalanceforhbmodel.count > 0) {
        return self.viewModel.managerIntegral.seg_segbalanceforhbmodel.count;
    }else if ((self.banalceType == SEGBanalceType_Banalce_Cash || self.banalceType == SEGBanalceType_Banalce_Time) && self.viewModel.dataSource.count > 0){
        return 1;
    }
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //零钱
    if (self.banalceType == SEGBanalceType_Cash) {
        SEGIntergralCashCell *cell = [SEGIntergralCashCell cellWithTableView:tableView WithIdentifier:SEGIntergralCashCellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.viewModel.dataSourceCash = self.viewModel.managerIntegral.seg_segbalanceforhbmodel;
        if (self.viewModel.dataSourceCash && self.viewModel.dataSourceCash.count > indexPath.row) {
            
            SEGBalanceForHbModel *data = self.viewModel.dataSourceCash[indexPath.row];
            
            [cell.iconImageView setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_pocketmoney_icon_01")]];
            CGFloat num = [data.amount floatValue] / 100.F;
            cell.cashLabel.text = [NSString stringWithFormat:@"%@%.2f",(num >= 0? @"+": @""),num];
            [cell.cashLabel setTextColor:kColor_1];
            cell.titleLabel.text = data.desc;
            cell.timeLabel.text = data.usedate;
        }
        
        return cell;
    }
    
    //优惠券
    SEGIntergralBalanceCell *cell = [SEGIntergralBalanceCell cellWithTableView:tableView WithIdentifier:SEGIntergralBalanceCellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.viewModel.dataSource && self.viewModel.dataSource.count > indexPath.section) {
        
        SEGBanalcesModel *data = self.viewModel.dataSource[indexPath.section];
        
        //展示图片
        
        [cell.iconImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kClientImageRootURL, data.providericon]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_70")]];
        //头部数据
        cell.titleLabel.text = data.title;
        
        //描述字段
        cell.cashLabel.text = data.providername;
        cell.timeLabel.text = [NSString stringWithFormat:@"有效期至%@",[SEGUtils dateFromString:data.exptime withFormatString:@"yyyy-MM-dd"]];
    }
    
    return cell;
    
}



//导航栏左侧显示返回按钮
- (void)showCustomNavigationBackButton
{
    //    common_btn_navbar_back_nor
    //导航栏的左边按钮
    UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"back")];
    UIButton* leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    [leftButton setImage:image forState:UIControlStateNormal];
    [leftButton setImage:image forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(onNavigationLeftButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}

//导航栏左侧显示返回按钮
- (void)showCustomNavigationBackButton1
{
    //    common_btn_navbar_back_nor
    //导航栏的左边按钮
    UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"back_white")];
    UIButton* leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    [leftButton setBackgroundImage:image forState:UIControlStateNormal];
    [leftButton setBackgroundImage:image forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(onNavigationLeftButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
}

- (void)onNavigationLeftButtonClicked:(UIButton*)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//显示导航栏右边的按钮
- (void)showCustomNavigationRightButtonWithTitle:(NSString*)aTitle image:(UIImage*)aImage hightlightImage:(UIImage*)hImage
{
    //导航栏的左边按钮
    UIButton* rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, aImage.size.width, aImage.size.height)];
    
    if (aTitle) {
        
        [rightButton setTitle:aTitle forState:UIControlStateNormal];
        rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        NSDictionary* attribute = @{ NSFontAttributeName : rightButton.titleLabel.font };
        CGSize textSize = [aTitle boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
        
        rightButton.frame = CGRectMake(0, 0, textSize.width, 41);
        [rightButton setTitleColor:kColor_W forState:UIControlStateNormal];
        [rightButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -13)];
    }
    
    [rightButton setBackgroundImage:aImage forState:UIControlStateNormal];
    if (hImage) {
        [rightButton setBackgroundImage:hImage forState:UIControlStateHighlighted];
    }
    
    [rightButton addTarget:self action:@selector(onNavigationRightButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
    if (@available(iOS 13.0, *)) {
        self.navigationController.navigationBar.standardAppearance.backgroundColor = kColor_S1;
    }
    
    
}

- (void)onNavigationRightButtonClicked:(UIButton*)sender
{
    NSDictionary *params = @{@"linkURL":kIntegarlHelpCenter};
    UIViewController *WebViewLoadVC = [[SEGUhomeMediator sharedInstance] seg_initWebViewLoadController:params];
    [self.navigationController pushViewController:WebViewLoadVC animated:YES];
}
#pragma mark - init Views
-(void)segInitViews{
    [self s1];
}
/// 初始化界面
-(void)s1{
    
    
    if (self.banalceType == SEGBanalceType_Cash) {
        self.title = @"零钱";
        [self loadBalanceForHb];
    }else if (self.banalceType == SEGBanalceType_Banalce_Cash||self.banalceType == SEGBanalceType_Banalce_Time) {
        self.title = @"优惠券";
        [self loadData_Integral];
        [self showCustomNavigationBackButton];
        
    }else if (self.banalceType == SEGBanalceType_Integral) {
        
        self.title = @"积分";
        [self showCustomNavigationBackButton];
        [self showCustomNavigationRightButtonWithTitle:@"帮助中心" image:nil hightlightImage:nil];
        [self headView];
        [self integralView];
        [self loadDataintegral];
        totalLabel.text = @"积分总额";
        [totalLabel sizeToFit];
    }
}
#pragma mark - RAC Data Binding
- (void)bindViewModel {
}

#pragma mark - getter
-(SEGMenberPointsViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [SEGMenberPointsViewModel new];
        _viewModel.banalceType = self.banalceType;
    }
    return _viewModel;
}
@end

